package com.xmy.cultivate.controller;

import com.xmy.cultivate.entity.SubBusiness;
import com.xmy.cultivate.entity.SubBusinessImages;
import com.xmy.cultivate.service.ISubBusinessImagesService;
import com.xmy.cultivate.service.ISubBusinessService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 子商户 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-06-02
 */
@RestController
@RequestMapping("/api/subBusiness")
public class SubBusinessController {

    @Autowired
    ISubBusinessService iSubBusinessService;

    @Autowired
    ISubBusinessImagesService iSubBusinessImagesService;

    @Autowired
    IdWorker idWorker;

    @PostMapping()
    @Transactional
    public Result save(@RequestBody SubBusiness subBusiness){
        subBusiness.setId(idWorker.nextId());
        boolean rs = iSubBusinessService.save(subBusiness);
        for(SubBusinessImages subBusinessImages:subBusiness.getSubBusinessImagesList()){
            subBusinessImages.setId(idWorker.nextId());
            subBusinessImages.setSubBusinessId(subBusiness.getId());
            iSubBusinessImagesService.save(subBusinessImages);
        }
        return new Result(rs? Code.OK:Code.ERROR,"","");
    }
}
